package controller;

import helpers.RealEstateHelper;
import helpers.SolrHelper;
import helpers.StringHelper;
import helpers.VnProvincesHelper;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import debug.RealEstateDebug;

import model.Province;
import model.RealEstate;
import model.SearchParam;
import model.SearchResult;

public class SearchAdvance extends HttpServlet {
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		ArrayList<String> typeRealEstate = RealEstateHelper.getTypeRealEstate();
		ArrayList<String> typeService = RealEstateHelper.getTypeService();
		ArrayList<Province> provinces = VnProvincesHelper.getProvinces();
		
		request.setAttribute("typeRealEstate", typeRealEstate);
		request.setAttribute("typeService", typeService);
		request.setAttribute("provinces", provinces);
		try {
			request.getRequestDispatcher("/pages/search_advance.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		String key = request.getParameter("key");
		String dateFrom = request.getParameter("from");
		String dateTo = request.getParameter("to");
		String typeService = request.getParameter("typeService");
		String typeRealEstate = request.getParameter("typeRealEstate");
		String province = request.getParameter("province");
		String district = request.getParameter("district");
		
		SearchParam searchParam = new SearchParam();
		
		//Check empty
		if (!key.isEmpty()) searchParam.setKey(key);
		if (!dateFrom.isEmpty()) searchParam.setDateFrom(dateFrom);
		if (!dateTo.isEmpty()) searchParam.setDateTo(dateTo);
		if (!typeService.isEmpty()){
			searchParam.setTypeService(typeService);
		}
		
		if (!typeRealEstate.isEmpty()) {
			searchParam.setTypeRealEstate(typeRealEstate);
		}
		
		if (!province.isEmpty()) {
			searchParam.setProvince(province);
		}
		
		if (!district.isEmpty()) {
			searchParam.setDistrict(district);
		}
		
		SearchResult sr = (SearchResult) SolrHelper.search(searchParam);
		
		response.getWriter().println("Total result: " + sr.getTotalResult());
		response.getWriter().println("Query time: " + sr.getQueryTime());
		response.getWriter().println("Key search: " + key);
		response.getWriter().println("-------------------------------------------------");
		
		ArrayList<RealEstate> realEstates = (ArrayList<RealEstate>) sr.getListRealEstate();
		if (realEstates.size() > 0) {
			for(int i = 0; i < realEstates.size(); i++) {
				RealEstateDebug.debugWeb(realEstates.get(i), response);
				response.getWriter().println("-------------------------------------------------");
				response.getWriter().println("");
				response.getWriter().println("");
			}
		}
	}
}
